<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/29
 * Time: 11:07
 */
namespace app\apiteacher\controller;
use think\Db;
class Online extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $user_id2 = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($user_id2,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    //学科分类
    public function itemize(){
        $res = Db::table('up_subject')->order('subject_sort asc')->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //在线老师
    public function teacher(){
        $page = input('param.page')?input('param.page'):1;
        $subject_id = input('param.subject_id');
        $num = 10;
        $limit = $page-1;
        $where['user_subject'] = $subject_id;
        $where['user_type'] = 2;
        $where['user_status'] = 2;
        $where['user_online'] = 1;
        $res = Db::table('up_u_user')->where($where)
            ->field('user_intro,user_sheng,user_grade,user_gender,user_id,user_username')
            ->limit($num*$limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            //地区
            $address = Db::table('up_area')->where('district_id',$vo['user_sheng'])->value('district');
            //星级
            $star = $this->teacherStar($vo['user_id']);
            //年级
            $grade = Db::table('up_grade')->where('grade_id',$vo['user_gender'])->value('grade_name');
            $vo['address'] = $address;
            $vo['star'] = $star;
            $vo['grade'] = $grade;
        }
        return json(['status' => 200, 'msg' => $res]);
    }
     //提问
    public function question(){
        extract(input());
        $subject_id  = input('param.subject_id');
        $course_type  = input('param.course_type');
        $course_content  = input('param.course_content');
        $couse_img  = input('param.couse_img');
        $course_gold  = input('param.course_gold');
        $course_teacher_id  = input('param.course_teacher_id');
        $data['course_user_id'] = $user_id;
        $data['course_subject_id'] = $subject_id;
        $data['course_type'] = $course_type;
        if($course_type==1){
        $data['course_teacher_id'] = $course_teacher_id;
        }
        $data['course_content'] = $course_content;
        $data['couse_img'] = $couse_img;
        $data['course_gold'] = $course_gold;
        $data['course_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_course')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //选择老师
    public function selectTeacher(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['user_type'] = 2;
        $where['user_status'] = 2;
        $where['user_online'] = 1;
        $res = Db::table('up_u_user')->where($where)
            ->field('user_id,user_username')
            ->limit($num*$limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //搜索老师
    public function keywordTeacher(){
        $page = input('param.page')?input('param.page'):1;
        $keyword = input('param.keyword');
        $res = array();
        if($keyword==''){
            return  json(array('status'=>200,'msg'=>$res));
        }
        $num = 10;
        $limit = $page-1;
        $where['user_type'] = 2;
        $where['user_status'] = 2;
        $where['user_online'] = 1;
        $where['user_username'] = array('like',"%$keyword%");
        $res = Db::table('up_u_user')->where($where)
            ->field('user_id,user_username')
            ->limit($num*$limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //一对一老师
    public function oneTeacher(){
        $page = input('param.page')?input('param.page'):1;
        $subject_id = input('param.subject_id');
        $num = 10;
        $limit = $page-1;
        $where['user_subject'] = $subject_id;
        $where['user_type'] = 2;
        $where['user_status'] = 2;
        $where['user_out'] = 1;
        $res = Db::table('up_u_user')->where($where)
            ->field('user_intro,user_sheng,user_grade,user_gender,user_id,user_username,user_learn_status')
            ->limit($num*$limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            //地区
            $address = Db::table('up_area')->where('district_id',$vo['user_sheng'])->value('district');
            //星级
            $star = $this->teacherStar($vo['user_id']);
            //年级
            $grade = Db::table('up_grade')->where('grade_id',$vo['user_gender'])->value('grade_name');
            $vo['address'] = $address;
            $vo['star'] = $star;
            $vo['grade'] = $grade;
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    //老师详情
    public function teacherDetail(){
        extract(input());
        $id = input('param.id');
        $where['user_id'] = $id;
        $res = Db::table('up_u_user')->where($where)
            ->field('user_intro,user_sheng,user_grade,user_gender,
            user_id,user_username,user_learn_status,user_good_at,
            user_exper,user_honor_img,user_toll,user_tcahe_style,user_subject')
            ->find();
        $map['follow_user_id'] = $user_id;
        $map['follow_quilt_user_id'] = $id;
        $info = Db::table('up_follow_user')->where($map)->find();
        if($info['follow_id']!=''){
            $res['is_dz'] = 1;
        }else{
            $res['is_dz'] = 2;
        }
        //星级
        $star = $this->teacherStar($res['user_id']);
        //年级
        $grade = Db::table('up_grade')->where('grade_id',$res['user_grade'])->value('grade_name');
        //教学特色
        $style_name = Db::table('up_teach_style')->where('style_id',$res['user_tcahe_style'])->value('style_name');
        //科目
        $subject_name = Db::table('up_subject')->where('subject_id',$res['user_subject'])->value('subject_name');
        $res['star'] = $star;
        $res['grade'] = $grade;
        $res['style_name'] = $style_name;
        $res['subject_name'] = $subject_name;
        return json(['status' => 200, 'msg' => $res]);
    }
    //关注，取消关注
    public function like(){
        extract(input());
        $id = input('param.id');
        $map['follow_user_id'] = $user_id;
        $map['follow_quilt_user_id'] = $id;
        $info = Db::table('up_follow_user')->where($map)->find();
        if($info['follow_id']!=''){
            $res = Db::table('up_follow_user')->where($map)->delete();
            if($res){
                return  json(array('status'=>200,'msg'=>"取消成功"));
            }else{
                return  json(array('status'=>200,'msg'=>"取消失败"));
            }
        }else{
            $res = Db::table('up_follow_user')->insertGetId($map);
            if($res){
                return  json(array('status'=>200,'msg'=>"点赞成功"));
            }else{
                return  json(array('status'=>200,'msg'=>"点赞失败"));
            }
        }
    }
    //我要上课
    public function classes(){
        extract(input());
        $id = input('param.id');
        $data['one_student_id'] = $user_id;
        $data['one_teacher_id'] = $id;
        $data['one_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_one_to_one')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"申请成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"申请失败"));
        }
    }

    //申请
    public function apply(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['one_student_id'] = $user_id;
        $res = Db::table('up_one_to_one')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.one_teacher_id','left')
            ->field('uuu.user_username,a.*')
            ->where($where)
            ->limit($num*$limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //公开课
    public function classed(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $id= input('param.id');
        $where['public_teacher_id'] = $id;
        $res = Db::table('up_public_class')->where($where)
            ->limit($num*$limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
           $count = Db::table('up_public_apply')->where('apply_public_id',$vo['public_id'])->count();
            $vo['num'] = $count;
        }
        return  json(array('status'=>200,'msg'=>$res));

    }
}